

package examples.radio;

import commandz.cfg.Configuration;
import commandz.var.*;
import java.io.IOException;


/**
 *  A device to receive and decode wireless signals.
 *
 *  The properties of this class are Variable derivatives so that
 *  their values can be stored to, and loaded from, a file.
 *  This is accomplished by application of the Configuration class.
 *
 *  Native types could be used as properties to reduce memory consumption.
 *  A Configuration-object is then created (only when it's needed) by the
 *  methods load() and store(). This takes slighty more execution-time
 *  because of the required initialization of the Configuration object.
 *  It's always a choice between space and time.
 *
 *  This is just an example.
 */
public final class Radio
{
    private VariableBoolean fm;
    private VariableReal frequency;
    private VariableInteger volume;
    private Configuration config;


    /**
     *  Initializes a new Radio-object into a default state.
     */
    public Radio()
    {
        this.fm = new VariableBoolean(false);
        this.frequency = new VariableReal(88);
        this.volume = new VariableInteger(2);

        this.config = new Configuration();
        this.config.add("fm", this.fm);
        this.config.add("frequency", this.frequency);
        this.config.add("volume", this.volume);
    }


    public String getModulation()
    {
        return this.fm.getValue() ? "FM" : "AM";
    }


    public float getFrequency()
    {
        return this.frequency.getValue();
    }


    public int getVolume()
    {
        return this.volume.getValue();
    }


    public void setFrequencyModulation(final boolean fm)
    {
        this.fm.setValue(fm);
    }


    public void setFrequency(final float frequency)
    {
        this.frequency.setValue(frequency);
    }


    public void setVolume(final int volume)
    {
        this.volume.setValue(volume);
    }


    public void load(final String filename) throws IOException
    {
        this.config.read(filename);
    }


    public void store(final String filename) throws IOException
    {
        this.config.write(filename);
    }

}
